home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 015 / epsilon.arc / VERSIONS.ARC / VERSIONS.E < prev    next >
Text File  |  1986-04-06  |  4KB  |  181 lines

  1. /*
  2.  *  Version numbering for EPSILON.
  3.  *
  4.  *  Copyright (c) 1986 by David Dyer-Bennet
  5.  *  Permission for non-commercial use is hereby granted; all other
  6.  *  rights are reserved.
  7.  *
  8.  *  Written by David Dyer-Bennet
  9.  *  Terrabit Software
  10.  *  4242 Minnehaha Ave S
  11.  *  Minneapolis, MN 55406
  12.  *  Sysop of Fido 14/341, The Terraboard, (612) 721-8967 3/12/24 24hrs
  13.  *  (612) 721-8800 NOT 24 hrs!  More like noon to midnight
  14.  */
  15.  
  16. /*
  17.  *  For certain file types (identified by setting buffer int vers_edit
  18.  *  in their mode routines) this provides support for multiple versions
  19.  *  of the file to exist at once.  The file foo.mss will contain
  20.  *  the latest version of the file; old versions of the
  21.  *  file will be in foo.m01, foo.m02, etc. foo.mzz will contain information
  22.  *  about the file ("m" is specified in
  23.  *  the master file).
  24.  *
  25.  *  The format of this master file is the same as an environment table.
  26.  */
  27.  
  28. /*
  29.  *  Revision history:
  30.  *
  31.  *      Edit    Date        Who     Description
  32.  *
  33.  *  Version T1.0 (Alpha test)
  34.  *      1       5-Apr-86   DD-B    Initial creation
  35.  *      2       6-Apr-86   DD-B    Keep latest version in normal name
  36.  */
  37.  
  38. #include <eel.h>
  39. #include "versions.h"
  40.  
  41. int getpsenv (name)
  42. char *name;
  43. /*
  44.  *  Get the value of an environment variable stored in the current buffer.
  45.  *  (Returns pointer to start);
  46.  */
  47. {
  48.      point = 0;
  49.      if ((!search (1, name)) || (!search (1, "="))) {
  50.      return NULL;
  51.      }
  52.      while (isspace (curchar ())) point++;    /* Skip over space */
  53.      return point;
  54. }
  55.  
  56. int atoi (s)
  57. char *s;
  58. {
  59.     int i = 0;
  60.     
  61.     while (isspace (*s)) s++;
  62.     while (isdigit (*s))
  63.     i = i * 10 + *s++ - '0';
  64.     return i;
  65. }
  66.  
  67. versions_edit (extchar)
  68. char extchar;
  69. {
  70.     int i, vers;
  71.     char verstr [10], *oldbuf, mbuf [FNAMELEN], vmast [FNAMELEN];
  72.  
  73.     /*
  74.      *  Find proper data file based on EXTCHAR and CURVER parameters in
  75.      *  master file.
  76.      */
  77.  
  78.     /* Remember extension character */
  79.     vers_extchar = extchar;
  80.     strcpy (vers_name, filename);
  81.     
  82.     /* Make up master file name */
  83.     strcpy (vmast, filename);
  84.     i = get_extension (vmast) - vmast;
  85.     i += 1;            /* Leave the dot */
  86.     vmast [i++] = extchar;
  87.     strcpy (&vmast [i], "ZZ");
  88.  
  89.     /* Grab master file into a buffer */
  90.     strcpy (mbuf, bufname);
  91.     strcat (mbuf, "<V>");
  92.     zap (mbuf);
  93.     oldbuf = bufname;
  94.     bufname = mbuf;
  95.     filename = vmast;
  96.     if (file_read (vmast, 1)) {
  97.     stuff ("CURVER = 0\n");
  98.     vers = -1;
  99.     } else {
  100.     if (!getpsenv ("CURVER")) {
  101.         error ("\"CURVER\" not found in %s", filename);
  102.     }
  103.     i = point;
  104.     nl_forward (); point--;
  105.     grab (i, point, verstr);
  106.     vers = atoi (verstr);
  107.     if (vers == 99) {
  108.         error ("File version wraparound, time to purge");
  109.     }
  110.     }
  111.     
  112.     bufname = oldbuf;
  113.     vers_version = vers + 1;
  114. }
  115.  
  116. int versions_save ()
  117. /*
  118.  *  Called by save_file before the file connected to the buffer is
  119.  *  actually saved, if the file is a versioned file and if the file
  120.  *  name matches the versioned name.  The purpose of this routine
  121.  *  is to update the master file.
  122.  *
  123.  *  On entry the buffer being saved will be current.
  124.  */
  125. {
  126.     int i, vers;
  127.     char *oldbuf, mbuf [FNAMELEN], oldfn [FNAMELEN], verstr [3], *p;
  128.  
  129.     /* Make up name for next version of old file */
  130.     if (vers_version >= 0) {
  131.     strcpy (oldfn, filename);
  132.     p = get_extension (oldfn);
  133.     p += 1;            /* Skip over dot */
  134.     *p++ = vers_extchar;
  135.     sprintf (p, "%02.2d", vers_version - 1);
  136.     rename_file (filename, oldfn);    /* Rename old version */
  137.     }
  138.     vers = vers_version;
  139.     
  140.     /* Update the master file */
  141.     oldbuf = bufname;
  142.     strcpy (mbuf, bufname);
  143.     strcat (mbuf, "<V>");
  144.     bufname = mbuf;
  145.  
  146.     if (!getpsenv ("CURVER")) {
  147.     error ("\"CURVER\" not found in %s", filename);
  148.     }
  149.  
  150.     i = point;
  151.     nl_forward ();
  152.     point--;
  153.     delete (i, point);
  154.     bprintf ("%d", vers);
  155.     file_write (filename, 1);
  156.     bufname = oldbuf;
  157.     
  158.     vers_version += 1;
  159. }
  160.  
  161. command version_statistics ()
  162. {
  163.     char relname [FNAMELEN];
  164.     
  165.     if (vers_edit) {
  166.     relative (vers_name, relname);
  167.     say ("Version %d, Master %s, Ext %c", vers_version, relname,
  168.       vers_extchar);
  169.     } else {
  170.     say ("Not versioned");
  171.     }
  172. }
  173.  
  174. command un_version ()
  175. {
  176.     vers_edit = 0;
  177.     say ("Versioning disabled");
  178. }
  179.  
  180. /* End of versions.e */
  181.